package nero.yncc.com.createart.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.activity.ActivitySearchAllGod;
import nero.yncc.com.createart.bean.BeanAttr;
import nero.yncc.com.createart.custom.IconView;
import nero.yncc.com.createart.custom.MyRecyclerView;
import nero.yncc.com.createart.utils.MyLayoutManager;

/**
 * Created by nero on 17-6-26.
 */

public class AdapterSearchAll extends RecyclerView.Adapter<AdapterSearchAll.Holder> {
    Context context;
    ArrayList<BeanAttr> arrayList_all;
    ActivitySearchAllGod activitySearchAllGod;

    public AdapterSearchAll(Context context, ArrayList<BeanAttr> beanSearchAlls,ActivitySearchAllGod activitySearchAllGod) {
        this.context = context;
        this.arrayList_all = beanSearchAlls;
        this.activitySearchAllGod=activitySearchAllGod;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_search_all, parent, false);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final BeanAttr beanSearchAll = arrayList_all.get(position);

        holder.tv_title.setText(beanSearchAll.getName());

        if(TextUtils.isEmpty(beanSearchAll.getChooseName())){
            holder.tv_choose.setText("没有选择该条件");
            holder.tv_choose.setTextColor(context.getResources().getColor(R.color.textColorGray_deep));
        }else{
            holder.tv_choose.setText(beanSearchAll.getChooseName());
            holder.tv_choose.setTextColor(context.getResources().getColor(R.color.project_yellow));
        }
        holder.ll_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.rc_list.getVisibility() == View.GONE){
                    holder.rc_list.setVisibility(View.VISIBLE);
                    holder.icon_right.setText(R.string.icon_down);

                } else{
                    holder.rc_list.setVisibility(View.GONE);
                    holder.icon_right.setText(R.string.icon_right);
                }

            }
        });
//        GridLayoutManager manager = new GridLayoutManager(context, 3);
//        holder.rc_list.setLayoutManager(manager);

        MyLayoutManager manager=new MyLayoutManager(context);
        holder.rc_list.setLayoutManager(manager);

        //存放下面选项的arraylist
        final ArrayList<BeanAttr.BeanAttrIn> arrayList = (ArrayList<BeanAttr.BeanAttrIn>) beanSearchAll.getItem();

        AdapterSearchALlInText adapterSearchALlInText = new AdapterSearchALlInText(context, arrayList, new DochildrenClick() {
            @Override
            public void click(int position) {
                holder.tv_choose.setText(arrayList.get(position).getName());
                holder.tv_choose.setTextColor(context.getResources().getColor(R.color.project_yellow));
                beanSearchAll.setChooseName(arrayList.get(position).getName());
            }
        },activitySearchAllGod);
        holder.rc_list.setAdapter(adapterSearchALlInText);
        adapterSearchALlInText.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return arrayList_all.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView tv_title, tv_choose;
        IconView icon_right;
        MyRecyclerView rc_list;
        LinearLayout ll_click;
        public Holder(View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_choose = itemView.findViewById(R.id.tv_choose);
            icon_right = itemView.findViewById(R.id.icon_right);
            rc_list = itemView.findViewById(R.id.rc_list);
            rc_list = itemView.findViewById(R.id.rc_list);
            ll_click=itemView.findViewById(R.id.ll_click);

        }
    }

    public interface DochildrenClick {
        void click(int position);
    }

    public void reset(){
        for (BeanAttr bean : arrayList_all){
            bean.setChooseName("");
            for (BeanAttr.BeanAttrIn beanin:bean.getItem()) {
                beanin.setChoose(false);
            }
        }
    }
}
